package com.zeny.edu.ucenter.util;

import com.zeny.edu.ucenter.entity.Member;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author helen
 * @since 2019/3/16
 */
public class JwtUtils {


    public static final String APPSECRET = "online123456";

    /**
     * 生成jwt
     *
     * @param member
     * @return
     */
    public static String genJsonWebToken(Member member) {

        if (member == null
                || StringUtils.isEmpty(member.getId())
                || StringUtils.isEmpty(member.getNickname())
                || StringUtils.isEmpty(member.getAvatar())) {
            return null;
        }

        String token = Jwts.builder().setSubject("online")
                .claim("id", member.getId())
                .claim("nickname", member.getNickname())
                .claim("avatar", member.getAvatar())
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 30))
                .signWith(SignatureAlgorithm.HS256, APPSECRET).compact();

        return token;
    }

    /**
     * 校验jwt
     *
     * @param token
     * @return
     */
    public static Claims checkJwt(String token) {
        Claims claims = Jwts.parser().setSigningKey(APPSECRET).parseClaimsJws(token).getBody();
        return claims;
    }

    /**
     * 测试jwt的生成
     */
    private static void testGenJsonWebToken() {
        Member member = new Member();
        member.setId("999");
        member.setNickname("helen");
        member.setAvatar("http://hahaha/hahah.png");

        String token = JwtUtils.genJsonWebToken(member);
        System.out.println(token);
    }


    /**
     * 测试jwt的校验
     */
    public static void testCheckJwt() {

        String token = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJvbmxpbmUiLCJpZCI6Ijk5OSIsIm5pY2tuYW1lIjoiaGVsZW4iLCJhdmF0YXIiOiJodHRwOi8vaGFoYWhhL2hhaGFoLnBuZyIsImlhdCI6MTU4MzkyNjU1MSwiZXhwIjoxNTgzOTI4MzUxfQ.PuMdMZux0F5-YiIdqk-y7S44PPLpkmkEtS0OdIW3TNk";

        Claims claims = JwtUtils.checkJwt(token);
        String id = (String) claims.get("id");
        String nickname = (String) claims.get("nickname");
        String avatar = (String) claims.get("avatar");
        System.out.println(id);
        System.out.println(nickname);
        System.out.println(avatar);
    }


    public static void main(String[] args) {
        testGenJsonWebToken();

        testCheckJwt();
    }
}
